<?php 
class SearchesController extends AppController {
	
	var $name = 'Searches';
	
	
    function index() {
		$this->loadModel('Profession');
	    $this->loadModel('Learningform');
	    $this->loadModel('Learningtype');
	    $this->loadModel('Studentgroup');
		    
		$professions = $this->Studentgroup->Course->Profession->find('list');
	//	$courses = $this->Studentgroup->Course->find('list');
		/*$learningforms = $this->Studentgroup->Learningform->find('list');*/
		$learningtypes = $this->Studentgroup->Learningtype->find('list');
		$this->set(compact('professions', 'learningtypes'));
	}
	
	
	function findstudents() {
		//Пдгружаем модели
		$this->loadModel('Student');
		$this->loadModel('Gender');
		$this->loadModel('Citizenship');
		$this->loadModel('Educationline');
		$this->loadModel('Profession');
		$this->loadModel('Course');
		$this->loadModel('Studentgroup');
		$this->loadModel('Studentgroupstype');
		
		$students = array();
		$gender = array();
		$citizenship = array();
		$educationline = array();
		$course = array();
		$profession = array();
		$studentgroupstype = array();
		$conditionsearshe = array();  //массив с условиями поиска студентов
		$studentgroup_id = array();   //массив id групп
		
		if (isset($this->request->data['Searche']['studentgroup_id']) and $this->request->data['Searche']['studentgroup_id']) {
			$studentgroup_id = $this->request->data['Searche']['studentgroup_id'];
		} elseif (isset($this->request->data['Searche']['course_id']) and $this->request->data['Searche']['course_id']) {
			$course_id = $this->request->data['Searche']['course_id'];
			$studentgroup = $this->Studentgroup->find('all', array('conditions' => 
			array('Studentgroup.course_id' => $course_id),'recursive' => -1, 'order' => 'Studentgroup.id', 'fields' => array('Studentgroup.id')));
			//формируем массив id групп
			foreach ($studentgroup as $element):
			array_push($studentgroup_id, $element['Studentgroup']['id']);
			endforeach;
			//debug($studentgroup_id);
		} elseif (isset($this->request->data['Searche']['profession_id']) and $this->request->data['Searche']['profession_id']) {
			$profession_id = $this->request->data['Searche']['profession_id'];
			$course = $this->Course->find('all', array('conditions' => 
			array('Course.profession_id' => $profession_id),'recursive' => -1, 'order' => 'Course.id'));
			//формируем массив id курсов
			$course_id = array();
			foreach ($course as $element):
			array_push($course_id, $element['Course']['id']);
			endforeach;
			$studentgroup = $this->Studentgroup->find('all', array('conditions' => 
			array('Studentgroup.course_id' => $course_id),'recursive' => -1, 'order' => 'Studentgroup.id', 'fields' => array('Studentgroup.id')));
			//формируем массив id групп
			foreach ($studentgroup as $element):
			array_push($studentgroup_id, $element['Studentgroup']['id']);
			endforeach;
		}
		if ($studentgroup_id) {
			$conditionsearshe['Student.studentgroup_id'] = $studentgroup_id;
		}
	    
	    if (isset($this->request->data['Searche']['learningtype_id'])) {
			if ($this->request->data['Searche']['learningtype_id']) {
		        $learningtype_id = $this->request->data['Searche']['learningtype_id'];
		        $conditionsearshe['Student.learningtype_id'] = $learningtype_id;
			}
		}
		if (isset($this->request->data['Searche']['studentid'])) {
			if ($this->request->data['Searche']['studentid']) {
		        $student_id = $this->request->data['Searche']['studentid'];
		        $conditionsearshe['Student.id'] = $student_id;
			}
		}
	    if (isset($this->request->data['Searche']['fio'])) {
	    	if ($this->request->data['Searche']['fio']) {
		      $fio = $this->request->data['Searche']['fio'];
		      $conditionsearshe['Student.fio'] = $fio;
	    	}
		}
	    if (isset($this->request->data['Searche']['engfio'])) {
	    	if ($this->request->data['Searche']['engfio']) {
		      $eng_fio = $this->request->data['Searche']['engfio'];
		      $conditionsearshe['Student.eng_fio'] = $eng_fio;
	    	}
		}
		//debug($conditionsearshe);
		$students = $this->Student->find('all', array('conditions' => 
				   $conditionsearshe,'recursive' => 1, 'order' => 'Student.id'));
		//debug($students);
		$i = 0;
		$stinfos = array(); //массив с данными студентов, который передаем во вьюху
		foreach ($students as $student):
		$stinfo = array(); //массив с данными студента
		$stinfo['id'] = $student['Student']['id'];
		$stinfo['fio'] = $student['Student']['fio'];
		$stinfo['passport_birthdate'] = $student['Studentinfo'][0]['passport_birthdate'];
		$stinfo['studentgroup'] = $student['Studentgroup']['name'];
		$stinfo['learningtype'] = $student['Learningtype']['name'];
		$stinfo['studentstate'] = $student['Studentstate']['name'];
		$gender = $this->Gender->find('all', array('conditions' => 
				   array('Gender.id' => $student['Studentinfo'][0]['gender_id']),'recursive' => -1, 'order' => 'Gender.id'));
		$stinfo['gender'] = $gender[0]['Gender']['name'];
		$citizenship = $this->Citizenship->find('all', array('conditions' => 
				   array('Citizenship.id' => $student['Studentinfo'][0]['citizenship_id']),'recursive' => -1, 'order' => 'Citizenship.id'));
		$stinfo['citizenship'] = $citizenship[0]['Citizenship']['name'];
		$educationline = $this->Educationline->find('all', array('conditions' => 
				   array('Educationline.id' => $student['Studentinfo'][0]['educationline_id']),'recursive' => -1, 'order' => 'Educationline.id'));
	    $stinfo['educationline'] = $educationline[0]['Educationline']['name'];   
        $course = $this->Course->find('all', array('conditions' => 
				   array('Course.id' => $student['Studentgroup']['course_id']),'recursive' => -1, 'order' => 'Course.id'));
		$stinfo['course'] = $course[0]['Course']['name'];		   
		$profession = $this->Profession->find('all', array('conditions' => 
				   array('Profession.id' => $course[0]['Course']['profession_id']),'recursive' => -1, 'order' => 'Profession.id'));		   
		$stinfo['profession'] = $profession[0]['Profession']['name'];
        $studentgroupstype = $this->Studentgroupstype->find('all', array('conditions' => 
				   array('Studentgroupstype.id' => $student['Studentgroup']['studentgroupstype_id']),'recursive' => -1, 'order' => 'Studentgroupstype.id'));		   
		$stinfo['studentgroupstype'] = $studentgroupstype[0]['Studentgroupstype']['name'];
        array_push($stinfos,$stinfo);
		$i++;		   
		endforeach;		   
		$this->set('stinfos',$stinfos);
		   
	    
		
	}
	
    public function update_course () {
 		if(!empty($this->data['Searche']['profession_id'])) {
			$profession_id = $this->data['Searche']['profession_id'];
			$this->loadModel('Course');
			$options = $this->Course->find('list'  , array('conditions' => array ('Course.profession_id' => $profession_id)));
			$this->set(compact('options'));
			$this->render('update_course', 'blank');
		} 
	}
    public function update_studentgroup () {
 		if(!empty($this->data['Searche']['course_id'])) {
			$course_id = $this->data['Searche']['course_id'];
			$this->loadModel('Studentgroup');
			$options = $this->Studentgroup->find('list'  , array('conditions' => array ('Studentgroup.course_id' => $course_id)));
			$this->set(compact('options'));
			$this->render('update_studentgroup', 'blank');
		} 
	}
	
}

?>